﻿#include<stdlib.h>

//fork&delay
#include "unistd.h"
//open file
#include <fstream>
//memset
#include <string.h>

#include "../../headers/fuminux_graphics.h"

using namespace std;

void logo() {

    unsigned char buffer[600 * 480 * 3 + 54];
    memset(&buffer, 0, 600 * 480 * 3 + 54);
    ifstream bmp;
    pixel px;
    px.A = 255;
    int width = 600;
    int height = 480;
    int startx = maxx / 2 - (width / 2);
    int starty = maxy / 2 - (height / 2);
    int endx = maxx / 2 + (width / 2);
    int endy = maxy / 2 + (height / 2);
    int ptr = 54;
    pixel screen[maxx][maxy];
    //一共10帧 播放1秒
    for (int flash = 1; flash <= 10; flash++) {
        char path[32];
        sprintf(path, "/etc/fuminux/logo/%d.bmp", flash);
        //cout << path<<endl;
        bmp.open(path, std::ios::binary);
        bmp.read((char*)&buffer, 600 * 480 * 3 + 54);
        if (!bmp.is_open()) {
            cout << "can't open file" << endl;
            exit(0);
        }
        //10*10ms
        for (int i = 0; i <= 10; i++) {
            //10ms
            usleep(1000 * 10);
            for (int x = 1; x < maxx; x++) {
                for (int y = 1; y < maxy; y++) {
                    px.B = buffer[54];
                    px.G = buffer[55];
                    px.R = buffer[56];
                    screen[x][y] = px;
                }
            }
            for (int h = endy; h > starty; h--) {
                //cout << h << endl;
                for (int w = startx; w < endx; w++) {
                    px.B = buffer[ptr];
                    px.G = buffer[ptr + 1];
                    px.R = buffer[ptr + 2];
                    screen[w][h] = px;
                    ptr += 3;
                }
            }
            ptr = 54;
            for (int x = 1; x < maxx; x++) {
                for (int y = 1; y < maxy; y++) {
                    point(screen[x][y], x, y);
                }
            }
        }
        bmp.close();
    }
    //重复播放第10帧,持续4秒
    for (int i = 0; i <= 4 * 100; i++) {
        //1ms
        usleep(1000 * 10);
        for (int x = 1; x < maxx; x++) {
            for (int y = 1; y < maxy; y++) {
                point(screen[x][y], x, y);
            }
        }
    }
}


void service() {
    fstream srv;
    srv.open("/etc/init-scripts/service.sh");
    if (!srv.is_open()) {
        cout << "no service found in /etc/init-scripts!" << endl;
    }
    srv.close();
    system("/etc/init-scripts/service.sh>>/dev/null");
}


int main(int argc, char* argv[])
{

    getInfo();

    if (argc == 1) {
        cout << "need 1 argument" << endl;
    }
    else if (argc == 2) {
        if (strcmp(argv[1], "-show") == 0) {
            int f = fork();
            //新进程
            if (f < 0) {
                cout << "failed." << endl;
            }
            if (f == 0) {
                logo();
            }
            if (f > 0) {

                exit(0);
            }
        }
        else if (strcmp(argv[1], "-service") == 0) {
            int f = fork();
            //新进程
            if (f < 0) {
                cout << "failed." << endl;
            }
            if (f == 0) {
                service();
                system("clear");
            }
            if (f > 0) {
                exit(0);
            }
        }
        else if (strcmp(argv[1], "-help") == 0) {
            cout << "usage:" << endl;
            cout << "       -help :show this message" << endl;
            cout << "       -show :show logo on screen ,usually 10s" << endl;
            cout << "              default logo path: /etc/fuminux/logo" << endl;
        }
        else {
            cout << "bad opnions: " << argv[1] << endl;
        }
    }
    else {
        cout << "too many arguments. see " << argv[0] << " -help" << endl;
    }
    return 0;

}

